package com.tianqu.tms.www.car.controller;

import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.dto.CarPortCountDto;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.CarPortCount;
import com.tianqu.tms.www.car.viewhelper.CarStockCountListTopViewhelper;

/**
 * @author zhangxueliang 库存统计列表controller
 */
public class CarStockCountListTopController extends TmsController {
	private Logger logger = Logger
			.getLogger(CarStockCountListTopController.class);
	public static final String CARPORTLIST = "carPortList";

	@SuppressWarnings("unchecked")
	@Override
	public void execute(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			CarPortCount carPortCount = CarPortCount.getInstance();


			List<CarPortCountDto> list = carPortCount.loadCount();

			// 往list中set头显示内容
			// 目前库存：1500台，累计入库：15000台(2013年1月至今) | 今日入库 10台，本月入库 100台
			int nowCount = 0;
			int sumCount = 0;
			int tCount = 0;
			int mCount = 0;
			int stayCount = 0;
			Car car = Car.getInstance();
			nowCount = car.findCarCount(2);
			stayCount = car.findCarCount(1);
			sumCount = car.findCarCount(3);
			if (list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					CarPortCountDto dto = list.get(i);
//					int nc = transHToZero(dto.getUsedCarPort());
//					int one = transHToZero(dto.getSeasonOneInner());
//					int two = transHToZero(dto.getSeasonTwoInner());
//					int three = transHToZero(dto.getSeasonThreeInner());
//					int four = transHToZero(dto.getSeasonFourInner());
					int today = transHToZero(dto.getTodayInner());
					tCount = tCount + today;

					int month = transHToZero(dto.getMonthInner());
					mCount = mCount + month;
				}
			}
			// 给前台打个当前年份
//			int yy = Integer.parseInt(new SimpleDateFormat("yyyy")
//					.format(Calendar.getInstance().getTime()));// 年

			CarStockCountListTopViewhelper view = new CarStockCountListTopViewhelper();

			//view.setYears(String.valueOf(yy));

			view.setCarPortList(list);

			view.setNowCount(String.valueOf(nowCount));
			view.setLjCount(String.valueOf(sumCount));
			view.setTodayCount(String.valueOf(tCount));
			view.setMonthCount(String.valueOf(mCount));
			view.setStayCount(String.valueOf(stayCount));
			uc.removeAttribute(TmsConstants.VIEWHELP);
			uc.setAttribute(TmsConstants.VIEWHELP, view);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
	}

	/**
	 * 
	 */
	public void countStock(UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#countStock was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			//
			final CarPortCount carPortCount = CarPortCount.getInstance();
			final List<Integer> counts = carPortCount.countCarInfo();
			CarStockCountListTopViewhelper view = (CarStockCountListTopViewhelper)uc.getAttribute(TmsConstants.VIEWHELP);
			view.setCarInCount(counts.get(0));
			view.setCarOutCount(counts.get(1));
			view.setCarTotalInventory(counts.get(2));
			uc.setAttribute(TmsConstants.VIEWHELP, view);
			uc.setAttribute("carInCount", view.getCarInCount());
			uc.setAttribute("carOutCount", view.getCarOutCount());
			uc.setAttribute("carTotalInventory", view.getCarTotalInventory());
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
	}
	
	public void countCarInfoByFloor(UserContext uc, int floor) throws Exception {
		logger.debug(this.getClass().getName() + "#countCarInfoByFloor was called.");
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			//
			final CarPortCount carPortCount = CarPortCount.getInstance();
			final List<CarPortCountDto> cars = carPortCount.countCarInfoByFloor(floor);
			CarStockCountListTopViewhelper view = (CarStockCountListTopViewhelper)uc.getAttribute(TmsConstants.VIEWHELP);
			view.setCarPortList(cars);
			uc.setAttribute(TmsConstants.VIEWHELP, view);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#countCarInfoByFloor was completed.");
	}
	
	/**
	 * 将“-”转换成0
	 * 
	 * @return
	 */
	private int transHToZero(String str) {
		if ("-".equals(str)) {
			return 0;
		} else {
			return Integer.parseInt(str);
		}
	}
}
